package com.kh.statistics.video.core.statistics;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kh.statistics.video.constant.PlatformConstant;
import com.kh.statistics.video.core.MediaContext;
import com.kh.statistics.video.core.Platform;
import com.kh.statistics.video.entity.StatisticsInfo;
import com.kh.statistics.video.entity.StatisticsMeta;
import com.kh.statistics.video.entity.StatisticsOne;
import com.kh.statistics.video.helper.ExcelHelper;
import com.kh.statistics.video.helper.NumberHelper;
import com.kh.statistics.video.helper.StatisticsHttpHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @BelongsProject: statistics
 * @BelongsPackage: com.kh.statistics.video.core.statistics
 * @Author: Hong.Kong
 * @CreateTime: 2024-05-10  09:38
 * @Description: TODO
 * @Version: 1.0
 */
@Component
@Slf4j
public class KuaiShouPlatform implements Platform {
    @Override
    public String support() {
        return PlatformConstant.KUAI_SHOU;
    }

    @Override
    public List<StatisticsInfo> getInfo(StatisticsMeta statisticsMeta) {
        return null;
    }

    @Override
    public void statistics(List<StatisticsMeta> list) {
        List<StatisticsInfo> statisticsInfoListAll = new ArrayList<>();
        for (StatisticsMeta statisticsMeta : list) {
            StatisticsInfo statisticsInfo = new StatisticsInfo();
            if ("keyMetrics".equals(statisticsMeta.getName())) {
            }
            statisticsInfoListAll.add(statisticsInfo);
        }
        ExcelHelper.export(ExcelHelper.getExcelName(list.get(0).getAccountName(), "快手统计总"), statisticsInfoListAll, StatisticsInfo.class);
    }

    @Override
    public void oneStatistics(List<StatisticsMeta> statisticsMetaList, MediaContext mediaContext) {
        List<StatisticsOne> statisticsInfoListAll = new ArrayList<>();
        Optional<StatisticsMeta> statistics = statisticsMetaList.stream().filter((item) -> null != item.getName() && "oneStatistics".equals(item.getName())).findFirst();
        if (statistics.isPresent()) {
            StatisticsMeta statisticsMeta = statistics.get();
            Map<String, String> headers = statisticsMeta.getHeaders();
            headers.put("Referer", "https://cp.kuaishou.com/statistics/works");
            headers.put("Content-type", "application/json;charset=UTF-8");
            headers.put("Connection", "keep-alive");
            headers.put("Host", "cp.kuaishou.com");
            String jsonParam = statisticsMeta.getJsonParam();
            if (StringUtils.isNotBlank(jsonParam)) {
                String replaceJsonParam = jsonParam.replaceAll(" ", "");
                statisticsMeta.setJsonParam(replaceJsonParam);
            }
            String body = StatisticsHttpHelper.request(statisticsMeta);
            if (!handlerResponse(body)) {
                return;
            }
            List<StatisticsOne> statisticsInfos = oneStatistics(body);
            statisticsInfoListAll.addAll(statisticsInfos);
        }
        ExcelHelper.export(ExcelHelper.getExcelName(statisticsMetaList.get(0).getAccountName(), "快手单条统计"), statisticsInfoListAll, StatisticsOne.class);
    }

    @Override
    public boolean handlerResponse(String body) {
        return true;
    }

    private List<StatisticsOne> oneStatistics(String body) {
        List<StatisticsOne> itemList = new ArrayList<>();
        JSONObject jsonObject = JSONObject.parseObject(body);
        JSONObject data = jsonObject.getJSONObject("data");
        JSONArray array = data.getJSONArray("photoList");
        for (Object o : array) {
            StatisticsOne statisticsOne = new StatisticsOne();
            JSONObject item = (JSONObject) o;
            String title = item.getString("title");
            statisticsOne.setWorks(title);
            String publishTime = item.getString("publishTime");
            statisticsOne.setPushDate(publishTime);
            String playUrl = item.getString("playUrl");
            statisticsOne.setLink(playUrl);
            Long playCount = item.getLong("playCount");
            statisticsOne.setExposure(playCount);
            double completePlayRate = item.getDouble("completePlayRate");
            statisticsOne.setCompletionRate(NumberHelper.doubleFormat(completePlayRate));
            Long likeCount = item.getLong("likeCount");
            statisticsOne.setClicksNumber(likeCount);
            Long commentCount = item.getLong("commentCount");
            statisticsOne.setCommentsNumber(commentCount);
            itemList.add(statisticsOne);
        }
        return itemList;
    }
}
